package admin

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
	"zhuandan/utils"
)

// 供应商采购单已确认通知
func Caigoubdcsts(openid, title string, infosjbid, uid int64, token string) (data T) {
	if infosjbid > 0 {
		layout := "2006-01-02 15:04:05"

		// 获取当前时间
		now := time.Now()

		// 格式化时间
		formatted := now.Format(layout)
		//token := Accesstokengzh()
		//token := "85_D84Ozd9mirqvz0DjpKON6rtviQ_f-oRXHRxvwvvqPSlHAdBSn2XiyAUvmk7GhH9JwNpkPivOeRUvVRQdVnA3PEcC33QoUAKHyBVA92Gby6m5KA6ozFbIHqKpv2MTYLfAGAEJW"
		//fmt.Println(token)
		d := map[string]interface{}{
			"touser":      openid,
			"template_id": "uxjeShJlBgc80ltBWMDO_esUL1S-N_cB6yDDqDO5rnM",
			"url":         "http://yyb.suishilian.com/#/pages/OrderDetails/OrderDetails?Id=" + utils.Int64string(infosjbid),
			"data": map[string]interface{}{
				"first": map[string]interface{}{ //产品名称
					"value": "新询价单通知",
				},
				"keyword1": map[string]interface{}{ //合计数量
					"value": "************",
				},
				"keyword2": map[string]interface{}{ //制单人
					"value": formatted,
				},
				"keyword3": map[string]interface{}{ //制单人
					"value": title,
				},
				"keyword4": map[string]interface{}{ //制单人
					"value": 1,
				},
			},
		}
		dJson, _ := json.Marshal(d)
		sJson := string(dJson)
		//fmt.Println(sJson)
		r := HttpPostbdcs("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token, sJson, "", d, uid, infosjbid)
		return r
	}
	return T{}
	//fmt.Println(resp)
}

func HttpPostbdcs(url, params string, contentType string, retMap interface{}, uid, infosjbid int64) (data T) {
	if contentType == "" {
		contentType = "application/json"
		// "application/x-www-form-urlencoded"
	}
	//fmt.Println(params)
	resp, err := http.Post(url,
		contentType,
		strings.NewReader(params))
	if err != nil {
		fmt.Println(err)
	}

	defer func() {
		_ = resp.Body.Close()
	}()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println(err)
	}
	//fmt.Println(string(body))

	err = json.Unmarshal(body, retMap)
	if err != nil {
		fmt.Println(err)
	}

	r := T{}
	err = json.Unmarshal(body, &r)
	return r
}
